package pms.dao;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pms.om.Image;
import pms.om.Raum;
import pms.om.BewDat;

/**
 * The Data access class for boats. All Interaction with the database
 * regarding the entity bean Raum should be handled by this class!
 * @author josef@dr-schneeberger.de
 */
public class RaumDao extends HibernateDaoSupport {
	private PersonDao personDao;
	
	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	/**
	 * returns a single boat by its primary db key
	 * @param id
	 * @return a single Raum
	 */
	public Raum findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Raum) template.get(Raum.class, id);
	}
	public int getMaxNrOfId() {
		HibernateTemplate template = getHibernateTemplate();
		return (int) template.getMaxResults();
	}
	
	/**
	 * Returns all boats from the database.
	 * @return a list of Raum
	 * @see Raum
	 */
	@SuppressWarnings("unchecked")
	public List<Raum> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		
//		SessionFactory hibernateSessionFactory = template.getSessionFactory();
//		Session sess = hibernateSessionFactory.getCurrentSession();
//		SQLQuery sqlQuery = sess.createSQLQuery("SELECT * FROM tbl_raum");
//		List raeume = sqlQuery.list();
		
//		Criteria crit = sess.createCriteria(Raum.class);
//		List raeume = crit.list();
//		return raeume;
		
		return template.loadAll(Raum.class);
	}

	/**
	 * modifies an existing boat in the database which is selected by its primary key.
	 * @param id the primary key of the boat.
	 * @param name the new name for the boat
	 * @param raum number 
	 * @param klasse the class of the boat (e.g. "skiff")
	 * @see Raum
	 */
	public void modify(int id, String name, int personenAnzahl, String klasse) {
		HibernateTemplate template = getHibernateTemplate();
		Raum raum = (Raum) template.get(Raum.class, id);
		raum.setKlasse(klasse);
		raum.setName(name);
		raum.setpersonenAnzahl(personenAnzahl);
		template.saveOrUpdate(raum);
	}

	/**
	 * creates a new persistent boat in the database providing all the necessary details.
	 * @param name the name for the new boat
	 * @param raumnumber 
	 * @param klasse class
	 * @see Raum
	 */
	public void create(String iName, int iRaum, String iKlasse) {
		HibernateTemplate template = getHibernateTemplate();
		
		Raum image = new Raum(iName, iKlasse, iRaum);
		template.persist(image);
	}

	/**
	 * returns the list of Rooms, that are currently not completely filled.
	 * @return a List of Raum
	 */
	@SuppressWarnings("unchecked")
	public List<Raum> findFreie() {
		HibernateTemplate template = getHibernateTemplate();
		List<BewDat> AktuelleBewDat = template.find("from BewDat f where f.tStampGeht is null");
		List<Raum> alle = template.loadAll(Raum.class);
		List<Raum> temp = template.loadAll(Raum.class);
		List<Raum> result = new LinkedList<Raum>();
		List<Raum> belegteRaume = new LinkedList<Raum>();
		for (BewDat bewdat : AktuelleBewDat) {
			bewdat.getRaum().decrementPersonenAnzahl() ;
			temp.add(bewdat.getRaum());
		}
		for (Raum raum : temp)
		{
			if (raum.getpersonenAnzahl() <=0)
			{
				belegteRaume.add(raum) ;
			}
		}
		for (Raum raum : alle) {
			if (!belegteRaume.contains(raum)) {
				result.add(raum);
			}
		}
		return result;
	}

	/**
	 * deletes a boat using it's id.
	 * @param id
	 * @throws RaumDeleteException if there are BewDat objects involving the boat.
	 */
	public void deleteById(int id) throws RaumDeleteException {
		HibernateTemplate template = getHibernateTemplate();
		Raum b = findById(id);
		Set<BewDat> bewdaten = b.getBewDatenImg();
		
		try{
			if (bewdaten == null){
				template.delete(b);	} 
			else if (bewdaten.size() == 0) { //hier läuft er in einen Fehler:
				template.delete(b);	} 		//failed to lazily initialize a collection of role: pms.om.Raum.BewDatenImg, no session or session was closed 
			else if (bewdaten.size() == 0) {
				template.delete(b);	} 
			else {
				throw new RaumDeleteException();}
		}catch(Exception e) {throw new RaumDeleteException();}
	}
}